package com.zmn.plat.dubbo.impl.orderconfig.cancel;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cglib.beans.BeanCopier;

import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.common.dto.orderconfig.cancel.category.OrderCancelCategoryDIO;
import com.zmn.plat.common.dto.orderconfig.cancel.category.OrderCancelCategoryDRO;
import com.zmn.plat.dubbo.interfaces.orderconfig.cancel.OrderCancelCategoryListRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.entity.orderconfig.cancel.OrderCancelCategory;
import com.zmn.plat.model.entity.orderconfig.cancel.OrderCancelCategoryQuery;
import com.zmn.plat.services.interfaces.orderconfig.cancel.OrderCancelCategoryService;
import com.zmn.plat.utils.bean.BeanCopyUtils;

/**
 * @author tanbiao
 * @description：取消分类
 * @since 2019/10/21 18:17
 * @modifier
 **/
@Service(version = DubboConsts.INTERFACE_VERSION, validation = "true")
@Slf4j
public class OrderCancelCategoryListRemoteServiceImpl implements OrderCancelCategoryListRemoteService {
    private Logger logger = LoggerFactory.getLogger(OrderCancelCategoryListRemoteServiceImpl.class);
    private static final String TAG = "订单取消分类 dubbo 接口";
    @Resource
    private OrderCancelCategoryService orderCancelCategoryService;

    /**
     * 描述: 根据主键查询取消分类
     *
     * @param categId
     * @return OrderCancelCategoryDRO
     * @auther: tanbiao
     * @since: 2019/10/23 11:11
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<OrderCancelCategoryDRO> getById(Integer categId) {
        log.info("getById 参数：categId:{},",categId);

        OrderCancelCategoryDRO dro = new OrderCancelCategoryDRO();
        OrderCancelCategory category = orderCancelCategoryService.findByKey(categId);
        if (category == null) {
            logger.error(String.format("[%s]参数:[%s]对应的对象不存在", TAG, categId));
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_RESULT_NULL);
        }
        BeanCopyUtils.copyProperties(category, dro);
        return ResponseDTO.success(dro);
    }

    /**
     * 描述: 查询所有取消分类
     *
     * @return OrderCancelCategoryDRO
     * @auther: tanbiao
     * @since: 2019/10/23 11:11
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<OrderCancelCategoryDRO>> listAll() {
        OrderCancelCategoryDIO dio = new OrderCancelCategoryDIO();
        return listByQuery(dio);
    }

    /**
     * 描述: 根据状态查询取消分类
     *
     * @param status
     *            1 禁用 2 启用
     * @return OrderCancelCategoryDRO
     * @auther: tanbiao
     * @since: 2019/10/23 11:11
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<OrderCancelCategoryDRO>> listByStatus(Integer status) {
        log.info("listByStatus参数：status:{},",status);

        OrderCancelCategoryDIO dio = new OrderCancelCategoryDIO();
        dio.setStatus(status);
        return listByQuery(dio);
    }

    /**
     * 描述: 根据状态查询取消分类
     *
     * @param orderCancelCategoryDIO
     * @return OrderCancelCategoryDRO
     * @auther: tanbiao
     * @since: 2019/10/23 11:11
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<OrderCancelCategoryDRO>> listByQuery(OrderCancelCategoryDIO orderCancelCategoryDIO) {
        log.info("listByQuery：orderCancelCategoryDIO:{},",orderCancelCategoryDIO);

        OrderCancelCategoryQuery query = new OrderCancelCategoryQuery();
        BeanCopyUtils.copyProperties(orderCancelCategoryDIO, query);
        List<OrderCancelCategory> cancelCategories =
            Optional.ofNullable(orderCancelCategoryService.listByQuery(query)).orElse(Collections.emptyList());
        BeanCopier copier = BeanCopier.create(OrderCancelCategory.class, OrderCancelCategoryDRO.class, false);
        List<OrderCancelCategoryDRO> cancelCategoryDROList = cancelCategories.stream().map(orderCancelCategory -> {
            OrderCancelCategoryDRO dro = new OrderCancelCategoryDRO();
            copier.copy(orderCancelCategory, dro, null);
            return dro;
        }).collect(Collectors.toList());

        return ResponseDTO.success(cancelCategoryDROList);
    }

    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<VtDTO>> listVtByStatus(Integer status) {
        log.info("listVtByStatus：status:{},",status);

        OrderCancelCategoryQuery query = new OrderCancelCategoryQuery();
        query.setStatus(status);
        List<OrderCancelCategory> list = Optional.ofNullable(orderCancelCategoryService.listByQuery(query)).orElse(Collections.emptyList());

        List<VtDTO> items =
            list.stream().map(item -> new VtDTO(item.getCategId(), item.getName(), item.getStatus())).collect(Collectors.toList());

        return ResponseDTO.success(items);
    }
}
